/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;



import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Nguyen Hung
 */
public class MailUtil {
    public static void sendMail(String fromEmail, String toEmail, String subject, String body) {
        try {
            Properties pros = System.getProperties();
            pros.put("mail.smtp.host", "127.0.0.1");
            pros.put("mail.smtp.port", "25");
            //        pros.put("mail.smtp.starttls.enable", "true");
            Session session = Session.getDefaultInstance(pros);
            Message message = new MimeMessage(session);
            message.setSubject(subject);
            message.setText(body);
            message.setFrom(new InternetAddress(fromEmail));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            message.setSentDate(new Date());
            message.saveChanges();
            Transport.send(message);
            System.out.println("Message sent OK!");
        } catch (MessagingException ex) {
            Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

//    public static void sendMail(String fromEmail, String toEmail, String subject, String body){
//        try {
//            Properties props = System.getProperties();
//            props.put("mail.smtp.host", "gmail-smtp-in.l.google.com");
//            props.put("mail.smtp.port", "25");
//            props.put("mail.smtp.starttls.enable", "true");
//            final String login = "hangnga9589@gmail.com";
//            final String pwd = "thanhcong";
//            Authenticator pa = null;
//            if (login != null && pwd != null) {
//                props.put("mail.smtp.auth", "true");
//                pa = new Authenticator() {
//
//                    public PasswordAuthentication getPasswordAuthentication() {
//                        return new PasswordAuthentication(login, pwd);
//                    }
//                };
//            } // else: no authentication
//            Session session = Session.getInstance(props, pa);
//            Message msg = new MimeMessage(session);
//            msg.setFrom(new InternetAddress(fromEmail));
//            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
//            msg.setSubject(subject);
//            msg.setText(body);
//            msg.setHeader("X-Mailer", "LOTONtechEmail");
//            msg.setSentDate(new Date());
//            msg.saveChanges();
//            Transport.send(msg);
//            System.out.println("Message sent OK.");
//        } catch (Exception ex) {
//            Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex);
//        }
//
//
//    }
    public static void main(String[] args) {
        MailUtil.sendMail("billgates@microsoft.com", "hunguaf@aol.com", "Hello the recipients", "Hi, I'm Bill Gates. "
                + "You has recept an promotion from microsoft corperation! please contact +841689993082 to reciept your giff!! Thank You!");
    }

}
